<?php
/**
 *用户相关业务
 * User: raisound
 * Date: 2022/10/01
 */

namespace app\api\business;
use app\common\lib\Str;
use app\common\lib\Time;
use app\common\model\mysql\UserInfo as UserInfoModel;
use think\Exception;
use app\common\model\redis\RedisModel;
class UserInfoBus extends BaseBus
{


    /**
     * 用户列表获取
     * @param array $data
     * @return array
     * @throws Exception
     */
    public function listData(array $data)
    {
        try {
            if(empty($data) || !isset($data['page']) || !isset($data['limit'])){
                return false;
            }
            // 限定条件
            $where = array();
            if(!empty($data['id'])) {
                $where[]=['id', '=', $data['id']];
            }
            // 需要显示的字段，默认空全部显示
            $displayField=[];
            // 需要排除显示的字段，默认空全部显示,优先级大
            $hideField = ['status'];
            // 排序
            $order = [ "id"=>"desc"];
            // 分页
            $page = $data['page'];
            $limit =  $data['limit'];
            if($page==0 || empty($limit)){
                $page = 1;
            }
            if($data['limit'] ==0 || empty($data['limit'])){
                $limit = 10;
            }
            $userObj = new UserInfoModel();
            // 查询数据
            $res = $userObj->getListData($where,$displayField,$hideField,$order,$page,$limit);
            if(empty($res)){
                return array();
            }
            return $res->toArray();
        }catch (\Exception $e){
            exception($e,'UserInfoBus.listData');
        }
    }

    /**
     * 更新token时间
     * @param $token
     * @return bool
     * @throws Exception
     */
    public static function heartbeat($token){
        try{
            $RedisModel = new RedisModel();
            $token_pre = $RedisModel->getValue(config("redis.token_pre").$token);
            if($token_pre){
                $userInfo = json_decode($token_pre,true);
                $RedisModel->setexValue(config("redis.token_id").$userInfo['id'],$token,Time::userLoginExpiresTime());
                $RedisModel->setexValue(config("redis.token_pre").$token,$token_pre,Time::userLoginExpiresTime());
                return $userInfo['id'];
            }
            return false;
        } catch(\Exception $e) {
            exception($e,'UserInfoBus.heartbeat');
        }
    }

    /**
     * 根据账号查询用户信息
     * @param $user_number 账号
     * @return array|false
     */
    public static function getUserByUserNumber($user_number)
    {
        try{
            $userObj = new UserInfoModel();
            $where = [
                "number" => $user_number,
            ];
            $userInfo = $userObj->getUserByCriteria($where);
            // 验证用户是否删除
            if(empty($userInfo)){
                return false;
            }
            return $userInfo->toArray();
        } catch(\Exception $e) {
            exception($e,'UserInfoBus.getUserByUserNumber');
        }
    }

    /**
     * 根据用户ID查询用户信息
     * @param $id // 用户ID
     * @return array|false
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getUserByUserId($id)
    {
        try{
            $userObj = new UserInfoModel();
            $where = [
                "id" => $id,
            ];
            $userInfo = $userObj->getUserByCriteria($where);
            // 验证用户是否删除
            if(empty($userInfo)){
                return false;
            }
            return $userInfo->toArray();
        } catch(\Exception $e) {
            exception($e,'UserInfoBus.getUserByUserId');
        }
    }

}
